package com.example.day2;

import android.content.Intent;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.ToastUtils;
import com.example.day2.adapter.FragmentAdapter;
import com.example.day2.busi.home.HomeFragment;
import com.example.day2.busi.message.MessageFragment;
import com.example.day2.busi.my.MyFragment;
import com.example.day2.databinding.ActivityMainBinding;
import com.example.day2.mvvm.base.BaseViewActivity;
import com.example.day2.video.VideoFragment;
import com.example.day2.zhibo.StartActivity;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;

public class MainActivity extends BaseViewActivity<ActivityMainBinding> {

    @Override
    public void initView() {
        //加摄像头权限
        requestPermissions(new String[]{
                "android.permission.CAMERA",
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO" ,
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE" ,
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS" ,
                "android.permission.ACCESS_WIFI_STATE" ,
                "android.permission.CHANGE_WIFI_STATE" ,
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"
        },100);
        //消息上加未读消息
        BadgeDrawable badge = binding.bnv.getOrCreateBadge(R.id.msg);
        badge.setNumber(99);

        //总布局添加碎片
        ArrayList<Fragment> list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new VideoFragment());
        list.add(new MessageFragment());
        list.add(new MyFragment());
        FragmentAdapter fragmentAdapter = new FragmentAdapter(this,list);
        binding.vp.setAdapter(fragmentAdapter);

        //切换导航,联动vp
        binding.bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        binding.vp.setCurrentItem(0);
                        break;
                    case R.id.video:
                        binding.vp.setCurrentItem(1);
                        break;
                    case R.id.publish:
                      startActivity(new Intent(MainActivity.this, StartActivity.class));
                        break;
                    case R.id.msg:
                        binding.vp.setCurrentItem(2);
                        break;
                    case R.id.my:
                        binding.vp.setCurrentItem(3);
                        break;
                }

                return true;
            }
        });
        //滚动vp,联动底部导航
        binding.vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
               switch (position){
                   case 0:
                       binding.bnv.setSelectedItemId(R.id.home);
                       break;
                   case 1:
                       binding.bnv.setSelectedItemId(R.id.video);
                       break;
                   case 2:
                       binding.bnv.setSelectedItemId(R.id.msg);
                       break;
                   case 3:
                       binding.bnv.setSelectedItemId(R.id.my);
                       break;

               }
            }
        });


    }

    @Override
    public void initData() {

    }

    @Override
    public int bindLayoutId() {
        return R.layout.activity_main;
    }
}